package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.market.sys.common.Result;
import com.market.sys.entity.SubComment;
import com.market.sys.entity.User;
import com.market.sys.service.ISubCommentService;
import com.market.sys.service.IUserService;
import com.market.sys.util.EmptyUtil;
import com.market.sys.util.TimeUtil;
import com.market.sys.vo.SubCommentVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.market.sys.util.Constants.DOWNLOAD_URL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/subComment")
public class SubCommentController {

    @Resource
    private ISubCommentService subCommentService;

    @PostMapping("/createSubComment")
    public Result<?> createSubComment(@RequestBody Map<String,Object> paramMap){
        SubComment subComment = BeanUtil.fillBeanWithMap(paramMap, new SubComment(), false);
        subComment.setTime(TimeUtil.timeYmdhms(new Date()));
        subComment.setState(1);
        if (subCommentService.save(subComment)){
            return Result.success("发表成功");
        }else {
            return Result.error("系统繁忙，发表失败");
        }
    }

    @PostMapping("/getSubCommentListByCommentId")
    public Result<?> fetchSubCommentList(@RequestBody Map<String,Object> paramMap){
        Integer commentId = Integer.parseInt(String.valueOf(paramMap.get("parentCommentId")));
        Integer state = Integer.parseInt(String.valueOf(paramMap.get("state")));;
        List<SubCommentVO> subCommentVOS = subCommentService.fetchSubCommentList(commentId, state);
        return Result.success(subCommentVOS);
    }

    @PostMapping("/deleteSubComment")
    public Result<?> deleteSubComment(@RequestBody Map<String,Object> paramMap){
        if (subCommentService.remove(new LambdaQueryWrapper<SubComment>().eq(SubComment::getId, String.valueOf(paramMap.get("id"))))) {
            return Result.success("删除成功");
        }else {
            return Result.error("系统繁忙，删除失败");
        }
    }
}
